package page2;

import java.util.Scanner;

public class Ques1828 {
	
	public void go(){
		Scanner scan = new Scanner(System.in);
		String[] arr;
		int n;
		while(scan.hasNext()){
			n = scan.nextInt();
			arr = new String[n];
			for(int i = 0;i < n;i ++){
				arr[i] = scan.next();
			}
			for(String str : arr){
				System.out.println(Integer.toOctalString(Integer.valueOf(str, 16)));
			}
		}
	}
	
	public static void main(String[] args) {
		new Ques1828().go();
	}
}
